<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Models\Admin\OperationLog;
use App\User;
class AdminApiAuth
{
    /**
     * 判断是否登录中间件
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // $uid= Auth::guard('web')->user()->id;  
        // $id = Auth::id();

        // dd(\auth('web')->check());
        if (!\auth('adminapi')->check()) {
            // return redirect('admin/auth/login');
            $request->headers->set('Accept', 'application/json');
            // return failed('身份认证失败', 401);
            return response()->json(['code'=>30002,'message'=>'登录超时！','data'=>null]);
        }
        // return $next($request);
        // echo 2;die;
        //后台验证Token 
        $uid = Auth::guard('adminapi')->user()->id;  
        $user = User::where(array('id' => $uid))->first();
        // echo 1 ;die;
        if (!$uid) {
            $request->headers->set('Accept', 'application/json');
            // return failed('身份认证失败', 401);
            return response()->json(array('message'=>'登录超时!'), 401);
            // return abort(401);
            // return redirect('admin/auth/login');
        }elseif(empty($user)||$user->status==1){
            $request->headers->set('Accept', 'application/json');
            // return failed('身份认证失败', 401);
            return response()->json(array('message'=>'该账号已被禁止访问!'), 401);
        }
        return $next($request);
    }
}
